import { getDynamicTableMapping } from '../lib/dynamic-table-mapping';

// 从硬编码文件中提取的映射（用于验证）
const hardcodedMapping: Record<string, string> = {
  '表 C.2': 'table_c2',
  '表C.2': 'table_c2',
  '表 C.4': 'table_c4',
  '表C.4': 'table_c4',
  '表 C.6': 'table_c6',
  '表C.6': 'table_c6',
  '表 C.14': 'table_c14',
  '表C.14': 'table_c14',
  '表 C.18': 'table_c18',
  '表C.18': 'table_c18',
  '表 C.27': 'table_c27',
  '表C.27': 'table_c27',
  '表 C.30': 'table_c30',
  '表C.30': 'table_c30',
  '表 C.36': 'table_c36',
  '表C.36': 'table_c36',
  '表 C.81': 'table_c81',
  '表C.81': 'table_c81',
  '表 C.109-1': 'table_c109_1',
  '表C.109-1': 'table_c109_1',
  '表 C.144': 'table_c144',
  '表C.144': 'table_c144',
  '表 C.150': 'table_c150',
  '表C.150': 'table_c150',
  '表 C.173': 'table_c173',
  '表C.173': 'table_c173',
  '表 C.174': 'table_c174',
  '表C.174': 'table_c174',
  '表 C.178': 'table_c178',
  '表C.178': 'table_c178',
  '表 C.179': 'table_c179',
  '表C.179': 'table_c179',
  '表 C.183': 'table_c183',
  '表C.183': 'table_c183',
  '表 C.201': 'table_c201',
  '表C.201': 'table_c201',
  '表 C.202': 'table_c202',
  '表C.202': 'table_c202',
  '表 C.203': 'table_c203',
  '表C.203': 'table_c203',
  '表 C.204': 'table_c204',
  '表C.204': 'table_c204',
  '表 C.207': 'table_c207',
  '表C.207': 'table_c207',
  '表 C.208': 'table_c208',
  '表C.208': 'table_c208',
  '表 C.212': 'table_c212',
  '表C.212': 'table_c212',
};

async function validateDynamicMapping() {
  console.log('🔍 验证动态表名映射是否可以替代硬编码映射...\n');

  try {
    const dynamicMapping = getDynamicTableMapping();
    
    // 获取映射统计信息
    const stats = dynamicMapping.getMappingStats();
    console.log('📊 动态映射统计信息:');
    console.log(`  总映射条目: ${stats.totalMappings}`);
    console.log(`  覆盖表数量: ${stats.uniqueTables}`);
    console.log(`  缓存过期时间: ${stats.cacheExpiry.toLocaleString()}`);
    console.log('');

    // 显示样例映射
    console.log('📋 动态映射样例:');
    stats.sampleMappings.forEach(({displayName, tableName}) => {
      console.log(`  "${displayName}" -> "${tableName}"`);
    });
    console.log('');

    // 验证硬编码映射
    console.log('✅ 验证硬编码映射替代可行性:');
    const validation = dynamicMapping.validateHardcodedMappings(hardcodedMapping);
    
    if (validation.isValid) {
      console.log('  ✅ 所有硬编码映射都可以被动态映射替代！');
    } else {
      console.log('  ❌ 发现不匹配的映射:');
      
      if (validation.missingMappings.length > 0) {
        console.log(`    缺失映射 (${validation.missingMappings.length}个):`);
        validation.missingMappings.forEach(key => {
          console.log(`      "${key}" - 动态映射中不存在`);
        });
      }
      
      if (validation.conflictingMappings.length > 0) {
        console.log(`    冲突映射 (${validation.conflictingMappings.length}个):`);
        validation.conflictingMappings.forEach(({key, hardcoded, dynamic}) => {
          console.log(`      "${key}": 硬编码="${hardcoded}" vs 动态="${dynamic}"`);
        });
      }
    }
    console.log('');

    // 测试批量转换
    console.log('🔄 测试批量名称转换:');
    const testNames = {
      naming: '表 C.2',
      position: '表C.4',
      baseParams: '表 C.109-1'
    };
    
    const convertedNames = dynamicMapping.mapMultipleNames(testNames);
    Object.entries(convertedNames).forEach(([key, tableName]) => {
      console.log(`  ${key}: "${testNames[key as keyof typeof testNames]}" -> "${tableName}"`);
    });
    console.log('');

    // 性能测试
    console.log('⚡ 性能测试:');
    const startTime = Date.now();
    for (let i = 0; i < 1000; i++) {
      dynamicMapping.mapDisplayNameToTableName('表 C.2');
    }
    const endTime = Date.now();
    console.log(`  1000次查询耗时: ${endTime - startTime}ms`);
    console.log('');

    // 提供替代方案建议
    console.log('💡 代码重构建议:');
    console.log('  1. 使用 mapDisplayNameToTableName() 替代硬编码映射对象');
    console.log('  2. 在批量操作中使用 mapMultipleDisplayNames() 提高效率');
    console.log('  3. 动态映射支持缓存，性能优于每次查表');
    console.log('  4. 新增表时无需修改代码，自动支持');
    console.log('');

    // 生成替代代码示例
    console.log('📝 替代代码示例:');
    console.log('  // 替换前 (硬编码):');
    console.log('  const hardcodedMap = { "表 C.2": "table_c2", ... };');
    console.log('  const tableName = hardcodedMap[displayName] || displayName;');
    console.log('');
    console.log('  // 替换后 (动态映射):');
    console.log('  import { mapDisplayNameToTableName } from "../lib/dynamic-table-mapping";');
    console.log('  const tableName = mapDisplayNameToTableName(displayName);');

  } catch (error) {
    console.error('❌ 验证过程失败:', error);
    process.exit(1);
  }
}

// 如果直接运行此脚本
if (require.main === module) {
  validateDynamicMapping();
}